package com.dding.cms.base.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.dding.core.annotation.Service;
import org.dding.core.log.ILogger;
import org.dding.core.log.LoggerFactory;
import org.dding.core.util.StringUtils;
import org.dding.mvc.ActionSupport;
import org.dding.mvc.InvocationContext;
import org.dding.mvc.annotation.ActionMethod;
import org.dding.mvc.annotation.ActionMethod.ForwardMethod;
import org.javalite.activejdbc.LazyList;
import org.javalite.activejdbc.Model;

import com.dding.cms.base.entity.BaseModel;
import com.dding.cms.base.entity.Site;
import com.dding.cms.base.web.bo.SiteBO;
import com.dding.cms.base.web.util.Utils;


import static com.dding.cms.base.web.util.PersistenceUtil.*;

@Service(name = "siteAction")
public class SiteAction extends ActionSupport<Object> {

	ILogger logger = LoggerFactory.getLogger(getClass());

	@SuppressWarnings("rawtypes")
	private static Set siteColums;

	@ActionMethod(
			name = "list",
			successView = "list.jsp",
			errorView = "error.html",
			httpMethod = ActionMethod.HttpMethod.ALL)
	public void list(InvocationContext invocationContext) {
		@SuppressWarnings("unchecked")
		LazyList<Model> site = Site.findAll();
		List<SiteBO> list = new ArrayList<SiteBO>();
		for (Model model : site.toArray(new Model[0])) {
			SiteBO siteBo = (SiteBO) Utils.buildBO(model, SiteBO.class);
			list.add(siteBo);
		}
		invocationContext.getPageContext().setAttribute("result", list);
	}

	@ActionMethod(
			name = "add",
			successView = "list.do",
			errorView = "error.html",
			httpMethod = ActionMethod.HttpMethod.POST)
	public void add(InvocationContext invocationContext) {
		Map<String, String[]> siteMap = invocationContext.getParameterMap();
		Site site = new Site();
		for (Entry<String, String[]> entry : siteMap.entrySet()) {
			site.set(entry.getKey(), entry.getValue()[0]);
		}
		site.save();
	}

	@ActionMethod(
			name = "modify",
			successView = "modify.jsp",
			errorView = "error.html",
			httpMethod = ActionMethod.HttpMethod.GET)
	public void modify(InvocationContext invocationContext) {
		String id = invocationContext.getParameterValue("id");
		if (!StringUtils.isEmpty(id)) {
			Model model = Site.findById(id);
			SiteBO siteBo = (SiteBO) Utils.buildBO(model, SiteBO.class);
			invocationContext.getPageContext().setAttribute("bo", siteBo);
		}
	}

	@SuppressWarnings("unchecked")
	@ActionMethod(
			name = "update",
			successView = "list.do",
			errorView = "error.html",
			httpMethod = ActionMethod.HttpMethod.POST)
	public void update(InvocationContext invocationContext) {
		initColumns();
		updateBo(invocationContext, siteColums, BaseModel.class, "site_id");
	}

	@ActionMethod(
			name = "delete",
			errorView = "error.html",
			forwardMethod = ForwardMethod.FLUSH)
	public void delete(InvocationContext invocationContext) {
		deleteBo(invocationContext, Site.class, "site_id=?");
	}

	private void initColumns() {
		if (siteColums == null)
			siteColums = Site.getMetaModel().getColumnMetadata().keySet();
	}
}
